/*
 * FlashItem.java
 *
 * Created on August 18, 2007, 12:36 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package flashomatic;

/**
 *
 * @author Administrator
 */

import java.util.*;
import javax.microedition.lcdui.Font;

public class FlashItem {
  
  String simplified;
  String traditional;
  String pinyin;
  Vector definition;
  int definitionLines = 0;
  boolean known = false;
  boolean unknown = false;
  int seenCount = 0;
  
  public FlashItem(String t, String s, String p, String d, int width, Font font) {
    traditional = t;
    simplified = s;
    pinyin = p;
    System.out.println("Creating new flash item: "+t+s+p+d);
     
    String x= new String();
    StringBuffer y = new StringBuffer();
    
    for(int i=0; i<d.length();i++){
      
      if (d.charAt(i)=='/'){
     
        y.append(x.trim());
        
        if ((i < d.length() - 2) && i > 1) y.append(", ");
        
        x = new String();
        
      } else if (d.charAt(i) != '/') {
        x+=d.charAt(i);
      }
      
    }
    
    String z = new String(y);
    
    // Split in to lines

    definition = lineSplit(z, width);
 
    
    //System.out.println("element "+(String)definition.elementAt(0));
  }
  
  
   public Vector lineSplit(String a_string, int a_lineWidth) {
    
    int length = a_string.length();
    int curLen = 0;
    int index = 0;
    int begin = 0;
    int lastIndex = -1;
    
    Font font = Font.getFont(Font.FACE_SYSTEM,Font.STYLE_PLAIN,Font.SIZE_SMALL);
    
    Vector a_lines = new Vector(4);
    
    while(index < length) {
      curLen = 0;
      int lineIndex = 0;
      while((curLen < a_lineWidth) && (index < length)) {
        char ch = a_string.charAt(index++);
        if((ch == '\n') || (ch == '\r')) {
          break;
        }
        
        curLen += font.charWidth(ch);
        lineIndex++;
      }
      char ch = 0;
      int origlineIndex = lineIndex;
      if(index < length) {
        do
        {
          ch = a_string.charAt(--index);
          lineIndex--;
        }while((lineIndex > 0) && (ch != ' ') && (ch != '\n') && (ch != '\r') && (index>0));
        
        if(lineIndex == 0) {
          index = lastIndex+origlineIndex;
          lastIndex = index;
        } else {
          lastIndex = index;
          if((ch != '\n') && (ch != '\r'))
            index++;
        }
      }
      
      if(index > 0) {
        if(a_lines != null)
          a_lines.addElement(a_string.substring(begin, index).replace('\n', ' '));
      }
      
      if((ch == '\n') || (ch == '\r'))
        begin = index++;
      else
        begin = index;
    }
    return a_lines;
  }
  
  
  public void setKnown() {
    known = true;
  }
  
  public void setUnknown() {
    known = false;
  }
  
  public boolean isKnown() {
    return known;
  }
  
  public String getSimplified() {
    
    if (simplified != null) {
      return simplified;
    } else {
      return " x ";
    }
    
  }
  
  public String getTraditional() {
    
    if (traditional != null) {
      return traditional;
    } else {
      return " x ";
    }
    
  }
  
  public String getPinyin() {
    
    if (pinyin != null) {
      return pinyin;
    } else {
      return " x ";
    }
    
  }
  

  
  
}
